package cn.chinasyq.photoquan.user.mode;

import android.content.Context;

import com.alipay.sdk.encrypt.MD5;
import com.ta.internal.multipart.upload.HttpType;
import com.ta.utdid2.android.utils.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import cn.chinasyq.photoquan.NetworkConstant;
import cn.chinasyq.photoquan.ParamConstant;
import cn.chinasyq.photoquan.user.bean.GameOrder;
import cn.chinasyq.photoquan.user.bean.GameOrderPojo;
import cn.chinasyq.photoquan.user.bean.UserInfo;
import cn.chinasyq.photoquan.user.bean.UserInfoPojo;
import cn.chinasyq.photoquan.util.SharedPreferencesUtils;
import cn.master.volley.commons.JacksonJsonUtil;
import cn.master.volley.commons.VolleyHelper;
import cn.master.volley.models.pojo.Wrapper;
import cn.master.volley.models.response.handler.ResponseHandler;
import cn.master.volley.models.response.listener.ResolveJsonHelper;
import cn.master.volley.models.response.listener.ResponseForMultiPartListener;
import cn.master.volley.models.response.listener.ResponseListener;

/**
 * Created by 一搏 on 2015/11/23.
 */
public class UserMode {
    private static UserInfo info;


    /**
     * 上传头像
     *
     * @param c
     * @param file
     * @param handler
     */
    public static ResponseForMultiPartListener<Wrapper<UserInfo>> uploadUserHeadPhoto(Context c, File file, ResponseHandler handler) {
        String url = NetworkConstant.API_USER_IMAGE;
        HashMap<String, String> params = new HashMap<>();
        params.put(ParamConstant.POST_PARAMS_TOKEY, UserMode.getCurrentUserInfo(c).getToken());
        HashMap<String, File> files = new HashMap<>();
        try {
            files.put(ParamConstant.POST_PARAMS_IMAGE, file);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return UploadFileModel.uploadFilesAndParams(ResolveJsonHelper.resolveJson2POJO(UserInfoPojo.class), HttpType.HttpPost, url, files, params, handler);
    }

    public static ResponseForMultiPartListener<Wrapper<Object>> uploadAlbumPhoto(Context c, File file, ResponseHandler handler) {
        String url = NetworkConstant.API_USER_UPLOAD_IMAGE;
        HashMap<String, String> params = new HashMap<>();
        params.put(ParamConstant.POST_PARAMS_TOKEY, UserMode.getCurrentUserInfo(c).getToken()+"");
        HashMap<String, File> files = new HashMap<>();
        try {
            files.put(ParamConstant.POST_PARAMS_IMAGE, file);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return UploadFileModel.uploadFilesAndParams(ResolveJsonHelper.resolveAndIgnoreData, HttpType.HttpPost, url, files, params, handler);
    }

    public static ResponseForMultiPartListener<Wrapper<GameOrder>> uploadGamePhoto(Context c, File file, String uuid, String name, String title, String desc, ResponseHandler handler) {
        String url = NetworkConstant.API_GAME_PARICIPATE;
        HashMap<String, String> params = new HashMap<>();
        params.put(ParamConstant.POST_PARAMS_TOKEY, UserMode.getCurrentUserInfo(c).getToken());
        params.put(ParamConstant.POST_PARAMS_TITLE, title);
        params.put(ParamConstant.POST_PARAMS_UUID, uuid);
        params.put(ParamConstant.POST_PARAMS_USER_NAME, name);
        params.put(ParamConstant.POST_PARAMS_USER_DESC, desc);
        HashMap<String, File> files = new HashMap<>();
        try {
            files.put(ParamConstant.POST_PARAMS_IMAGE, file);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return UploadFileModel.uploadFilesAndParams(ResolveJsonHelper.resolveJson2POJO(GameOrderPojo.class), HttpType.HttpPost, url, files, params, handler);
    }
    public static ResponseForMultiPartListener<Wrapper<Object>> uploadGamePhoto( File file, String uuid,String token,ResponseHandler handler) {
        String url = NetworkConstant.API_MY_GAME_UPLOAD_REAL_PHOTO;
        HashMap<String, String> params = new HashMap<>();
        params.put(ParamConstant.POST_PARAMS_TOKEY, token);
        params.put(ParamConstant.POST_PARAMS_UUID, uuid);
        HashMap<String, File> files = new HashMap<>();
        try {
            files.put(ParamConstant.POST_PARAMS_IMAGE, file);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return UploadFileModel.uploadFilesAndParams(ResolveJsonHelper.resolveAndIgnoreData, HttpType.HttpPost, url, files, params, handler);
    }
    /**
     * 获取我的页面中的数据
     *
     * @param handler
     */
    public static void obtainMyProfile(String token, ResponseHandler handler) {
        String url = String.format(NetworkConstant.API_USER_PROFILE, token);
        ResponseListener<UserInfo> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveJson2POJO(UserInfoPojo.class));
        VolleyHelper.get(url, true, listener, listener);
    }

        public static void obtainGameOrderInfo(String token, String uuid,
                ResponseHandler handler) {
            String url = String.format(NetworkConstant.API_MY_GAME_SHOW, uuid,token);
            ResponseListener<GameOrder> listener = new ResponseListener<>(
                    handler,
                    ResolveJsonHelper.resolveJson2POJO(GameOrderPojo.class));
            VolleyHelper.invalidateCache(url);
            VolleyHelper.get(url, true, listener, listener);
        }
    public static void giveUpGame(String token, String uuid,
                                           ResponseHandler handler) {
        String url = String.format(NetworkConstant.API_MY_GAME_QUIT, uuid,token);
        ResponseListener<Object> listener = new ResponseListener<>(
                handler,
                ResolveJsonHelper.resolveAndIgnoreData);
        VolleyHelper.invalidateCache(url);
        VolleyHelper.delete(url, true, listener, listener);
    }
    public static void sendCaptcha(String phone, ResponseHandler handler) {
        String url = NetworkConstant.API_USER_GET_CAPTCHA;
        ResponseListener<Object> listener = new ResponseListener<>(handler, ResolveJsonHelper.resolveAndIgnoreData);
        HashMap<String, String> params = new HashMap<>();
        params.put(ParamConstant.POST_PARAMS_PHONE, phone);
        VolleyHelper.post(url, params, listener, listener);
    }
    public static void payGame(String uuid, String token, String type, ResponseHandler handler) {
        String url = NetworkConstant.API_USER_GAME_PAY;
        ResponseListener loginListener = new ResponseListener(handler, ResolveJsonHelper.resolveJson2String);
        HashMap<String, String> map = new HashMap<>();
        map.put(ParamConstant.POST_PARAMS_ITEM_TYPE, type);
        map.put(ParamConstant.POST_PARAMS_ITEM_UUID, uuid);
        map.put(ParamConstant.POST_PARAMS_TOKEY, token);
        VolleyHelper.post(url, map, loginListener, loginListener);
    }
    public static void register(String phone, String pass, String captcha, ResponseHandler handler) {
        String url = NetworkConstant.API_USER_REGISTOR;
        ResponseListener loginListener = new ResponseListener(handler, ResolveJsonHelper.resolveAndIgnoreData);
        HashMap<String, String> map = new HashMap<>();
        map.put(ParamConstant.POST_PARAMS_PHONE, phone);
        map.put(ParamConstant.POST_PARAMS_PASS_MD5, MD5.a(pass.getBytes()));
        map.put(ParamConstant.POST_PARAMS_CAPTCHA, captcha);
        VolleyHelper.post(url, map, loginListener, loginListener);
    }

    public static void resetPass(String phone, String pass, String captcha, ResponseHandler handler) {
        String url = NetworkConstant.API_USER_RESET_PWD;
        ResponseListener loginListener = new ResponseListener(handler, ResolveJsonHelper.resolveAndIgnoreData);
        HashMap<String, String> map = new HashMap<>();
        map.put(ParamConstant.POST_PARAMS_PHONE, phone);
        map.put(ParamConstant.POST_PARAMS_PASS_MD5, MD5.a(pass.getBytes()));
        map.put(ParamConstant.POST_PARAMS_CAPTCHA, captcha);
        VolleyHelper.post(url, map, loginListener, loginListener);
    }

    public static void login(String phone, String pass, ResponseHandler handler) {
        String url = NetworkConstant.API_USER_LOGIN;
        ResponseListener<UserInfo> loginListener = new ResponseListener(handler, ResolveJsonHelper.resolveJson2POJO(UserInfoPojo.class));
        HashMap<String, String> map = new HashMap<>();
        map.put(ParamConstant.POST_PARAMS_PHONE, phone);
        map.put(ParamConstant.POST_PARAMS_PASS_MD5, MD5.a(pass.getBytes()));
        VolleyHelper.post(url, map, loginListener, loginListener);
    }

    public static void save(Context c, UserInfo data) {
        info = data;
        try {
            new SharedPreferencesUtils(c, UserInfo.class.getCanonicalName()).commitString(data.getUuid(), JacksonJsonUtil.toJson(data));
        } catch (IOException e) {
            e.printStackTrace();
        }
        saveCurrentUser(c, info.getUuid());
    }

    private static final String CURRENT_USER_UUID = "current_user_uuid";

    private static void saveCurrentUser(Context c, String uuid) {
        new SharedPreferencesUtils(c, UserInfo.class.getCanonicalName()).commitString(CURRENT_USER_UUID, uuid);
    }

    private static String readCurrentUser(Context c) {
        return new SharedPreferencesUtils(c, UserInfo.class.getCanonicalName()).getString(CURRENT_USER_UUID, "");
    }

    public static boolean isLogin(Context c) {
        if (info == null) {
            info = read(c, readCurrentUser(c));
        }
        if (StringUtils.isEmpty(info.getToken())) {
            return false;
        }
        return true;
    }

    public static void logout(Context c) {
        if (info == null) {
            info = read(c, readCurrentUser(c));
        }
        info.setToken("");
        save(c, info);
    }

    public static UserInfo getCurrentUserInfo(Context c) {
        if (info == null) {
            info = read(c, readCurrentUser(c));
        }
        if (isLogin(c)) {
            return info;
        }
        return new UserInfo();
    }

    public static UserInfo read(Context context, String uuid) {
        UserInfo info;
        try {
            info = JacksonJsonUtil.getObjectMapper().readValue(new SharedPreferencesUtils(context, UserInfo.class.getCanonicalName()).getString(uuid, ""), UserInfo.class);
        } catch (IOException e) {
            info = new UserInfo();
        }
        return info;
    }

    public static void sendFeedback(Context c, String comment, ResponseHandler handler) {
        String url = NetworkConstant.API_USER_FEEDBACK;
        ResponseListener<Object> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveAndIgnoreData);
        HashMap<String, String> params = new HashMap<>();
        params.put(ParamConstant.POST_PARAMS_TOKEY, UserMode.getCurrentUserInfo(c).getToken() + "");
        params.put(ParamConstant.POST_PARAMS_CONTENT, comment);
        VolleyHelper.post(url, params, listener, listener);
    }

    public static void updateAttr(Context c, ResponseHandler handler, String attr_name, String str) {
        String url = NetworkConstant.API_USER_UPDATE_ATTR;
        ResponseListener<UserInfo> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveJson2POJO(UserInfoPojo.class));
        HashMap<String, String> params = new HashMap<>();
        params.put(ParamConstant.POST_PARAMS_TOKEY, UserMode.getCurrentUserInfo(c).getToken() + "");
        params.put(ParamConstant.POST_PARAMS_ATTR_NAME, attr_name);
        params.put(ParamConstant.POST_PARAMS_ATTR_VAL, str);
        VolleyHelper.put(url, params, listener, listener);
    }
}
